class Node {
  constructor(data) {
    this.data = data;
    this.next = null;
  }
}

class LinkedList {
  constructor() {
    this.headNode = null; // 头节点
    this.lastNode = null; // 尾节点
    this.size = 0; // 链表长度
  }

  insert(item, ind) {
    let insertNode = new Node(item);
    if (this.size === 0) {
      this.headNode = insertNode;
      this.lastNode = insertNode;
    } else if (ind === 0) {
      // 头部插入
      insertNode.next = this.headNode;
      this.headNode = insertNode;
    } else if (this.size === ind) {
      // 尾部插入
      this.lastNode.next = insertNode;
      this.lastNode = insertNode;
    } else {
      // 中间插入
      let prevNode = this.get(ind - 1);
      insertNode.next = prevNode.next;
      prevNode.next = insertNode;
    }
    this.size++;
  }

  remove(ind) {
    let removeNode = null;
    if (ind === 0) {
      // 删除头节点
      removeNode = this.headNode;
      this.headNode = this.headNode.next;
    } else if (ind === (this.size - 1)) {
      // 删除尾节点
      let prevNode = get(ind - 1);
      removeNode = prevNode.next;
      prevNode.next = null;
      this.lastNode = prevNode;
    } else {
      // 删除中间节点
      let prevNode = this.get(ind - 1);
      let nextNode = prevNode.next.next;
      removeNode = prevNode.next;
      prevNode.next = nextNode;
    }
    this.size --;
    return removeNode;
  }

  get(ind) {
    let temp = this.headNode;
    for (let i = 0; i< ind;i++) {
      temp = temp.next;
    }
    return temp;
  }

  output() {
    let temp = this.headNode;
    while(temp !== null) {
      console.log(temp.data)
      temp = temp.next;
    }
  }
}
let list = new LinkedList();
list.insert('a')
list.insert('b', 1)
list.insert('c', 2)
list.insert('d', 3)
list.insert('cc', 2)
list.remove(2)
list.output();
console.log(list.get(2))